package com.itdct.onflow.system.file.manager.upload

import com.itdct.onflow.core.config.OnflowConfig
import com.itdct.onflow.core.constant.FileStoreTypeConst
import com.itdct.onflow.core.exception.CommonException
import com.itdct.onflow.core.extend.logWarn
import com.itdct.onflow.system.file.entity.SysFileEntity
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.stereotype.Component
import java.io.File
import java.io.FileInputStream

/**
 * @author Zhouwx
 * @date 2025/9/24 10:44:05
 * @version 1.0
 * @description
 */
@Component
@ConditionalOnProperty(name = ["onflow.file.container-type"], havingValue = "local", matchIfMissing = true)
class LocalFileTransfer(
    val onflowConfig: OnflowConfig
) : BaseFileTransfer {
    override fun upload(sysFileEntity: SysFileEntity, file: File, deleteAfterUpload: Boolean) {
        // INFO: Zhouwx: 2025/9/24 本地上传，主要保存到本地服务器即可，不需要上传到第三方服务器
    }

    override fun download(storePath: String): FileInputStream {
        val filePath = onflowConfig.file.localPath + storePath
        val file = File(filePath)
        if (!file.exists()) {
            throw CommonException("文件不存在")
        }
        return FileInputStream(filePath)
    }

    override fun delete(sysFileEntity: SysFileEntity) {
        val storePath = sysFileEntity.storePath
        val filePath = onflowConfig.file.localPath + storePath
        if (File(filePath).exists()) {
            File(filePath).delete()
        } else {
            logWarn("文件不存在，路径为：$filePath")
        }
    }

    override fun getType(): String {
        return FileStoreTypeConst.LOCAL
    }
}